package com.sinoglobal.ningxia.frame;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bugtags.library.Bugtags;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.MainActivity;
import com.sinoglobal.ningxia.activity.vip.Login;
import com.sinoglobal.ningxia.annotation.InJectView;
import com.sinoglobal.ningxia.utils.EquipmentUtil;
import com.sinoglobal.ningxia.utils.FlyUtil;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.sinoglobal.ningxia.utils.MContants;
import com.sinoglobal.ningxia.utils.SharedPreferenceUtil;
import com.sinoglobal.ningxia.utils.TextUtil;
import com.sinoglobal.ningxia.utils.ToastBuilder;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.annotation.view.ViewInject;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Stack;

public abstract class AbstractActivity extends FragmentActivity {
    protected TextView title;
    public static int RESULT_DATA = 1;
    protected boolean is_run = true; // 在异步加载中是否判断网络 true: 是
    // 标题
    protected TextView titleView;
    protected ImageView templateButtonLeft; // 模板中左则Button,不使用模板时，此值为null;
    protected TextView templateButtonRight; // 模板中右则Button,不使用模板时，此值为null;
    protected ImageView microButtonRight; // 微视听使用控件
    @ViewInject(id = R.id.view_mainBody)
    protected LinearLayout mainBody; // 主体显示
    private Dialog dialog;
    // private ProgressDialog progressDialog;
    private Dialog messageDialog; //
    protected boolean isTemplate = true; // 是否使用模板,不使用模板时请按照模板方式编写，使用时则不需要。
    protected RelativeLayout titleRelativeLayout;
    private boolean isShowNoValue = false;
    protected View templateView;
    @ViewInject(id = R.id.load_id)
    private LinearLayout reLoadLayout;
    @ViewInject(id = R.id.reLoadTv)
    protected TextView reLoadTv;
    // private ImageView loadingIv;
    public boolean cached = false; // true为加载过缓存数据
    protected boolean scollAble = true;
    // private LinearLayout main;
    public boolean scollBack = false;
    protected String sendParams; // 请求参数
    @ViewInject(id = R.id.progres1)
    private ProgressBar pb;
    protected boolean finishAnim;
    private ProgressDialog pd;
    private static Stack<Activity> activityStack;// activity栈
    public Activity mActivity;
    public boolean isHideTitle = false;

    public boolean isHideTitle() {
        return isHideTitle;
    }

    public void setHideTitle(boolean hideTitle) {
        isHideTitle = hideTitle;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FlyUtil.addAppActivity(this);
        // FinalActivity.initInjectedView(this);

        mActivity = this;
        if (isTemplate) {
            setContentView(R.layout.template);
            titleRelativeLayout = (RelativeLayout) findViewById(R.id.title_iv_id);
            templateButtonLeft = (ImageView) findViewById(R.id.title_but_left);
            templateButtonRight = (TextView) findViewById(R.id.title_but_right);
            microButtonRight = (ImageView) findViewById(R.id.mico_but_right);
            // templateButtonRight.setBackgroundResource(R.drawable.button_style);
            titleView = (TextView) findViewById(R.id.title_text);
            mainBody = (LinearLayout) findViewById(R.id.view_mainBody);
            templateButtonLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

        }

        titleView = (TextView) findViewById(R.id.title_text);
        mainBody = (LinearLayout) findViewById(R.id.view_mainBody);
        pb = (ProgressBar) findViewById(R.id.progres1);
        reLoadLayout = (LinearLayout) findViewById(R.id.load_id);
        reLoadTv = (TextView) findViewById(R.id.reLoadTv);
    }

    @Override
    public void setContentView(int layoutResID) {
        if (!isHideTitle) {//是否隐藏头部 默认不隐藏
            if (layoutResID == R.layout.template) {
                super.setContentView(layoutResID);
            } else {
                if (mainBody != null) {
                    mainBody.removeAllViews();
                    mainBody.addView(this.getLayoutInflater().inflate(layoutResID, null));
                } else {
                    super.setContentView(layoutResID);
                }
            }
        } else {
            super.setContentView(layoutResID);
        }
    }

    /**
     * @author ty
     * @createdate 2012-10-25 下午4:22:51
     * @Description: 返回首页
     */
    protected void goHome() {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        // FlyUtil.intentForward(AbstractActivity.this, HomeActivity.class,
        // intent);
        AbstractActivity.this.finish();
    }

    // 判断是否登录并跳转到登录界面
    public boolean isLoginIntentLoginActivity() {
        if (TextUtil.stringIsNull(FlyApplication.userId)) {
            startActivity(new Intent(this, Login.class));
            overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            return false;
        }
        return true;
    }

    ;

    /**
     * @param message
     * @author ty
     * @createdate 2012-9-24 上午9:10:36
     * @Description: 显示Toast消息
     */
    public void showShortToastMessage(String message) {
        ToastBuilder.getIntance(getApplicationContext()).showToast(message);
        // Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    // /**
    // * @author sbk
    // * @createdate 2014-4-27 下午4:08:30
    // * @Description: 换背景
    // * @param type
    // *
    // */
    // public void setBg(boolean type) {
    // if (type) {
    // mainBody.setBackgroundResource(R.drawable.bg_left);
    // titleRelativeLayout.setBackgroundResource(R.drawable.night_main_top_bg);
    // } else {
    // mainBody.setBackgroundResource(R.drawable.main_bg);
    // titleRelativeLayout.setBackgroundResource(R.drawable.main_top_bg);
    // }
    // }

    /**
     * @author sbk
     * @createdate 2014-4-3 下午3:23:08
     * @Description: 显示加载框
     */
    public void showLoading() {
        reLoadLayout.setVisibility(View.VISIBLE);
        pb.setVisibility(View.VISIBLE);
        reLoadTv.setVisibility(View.GONE);
        // logoAnimation = (AnimationDrawable) loadingIv.getBackground();
        // if (loadingHandler == null) {
        // loadingHandler = new Handler();
        // }
        // loadingHandler.postDelayed(new Runnable() {
        // public void run() {
        // logoAnimation.start(); // 开始动画
        // }
        // }, 50);

    }

    /**
     * @return
     * @throws Exception
     * @author qiwx
     * @createdate 2014-10-11 上午10:22:47
     * @description 查找id
     * @params
     */
    public void autoInjectAllField(Activity activity) throws IllegalAccessException, IllegalArgumentException {
        // 得到Activity对应的Class
        Class clazz = this.getClass();
        // 得到该Activity的所有字段
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            // 判断字段是否标注InjectView
            if (field.isAnnotationPresent(InJectView.class)) {
                // 如果标注了，就获得它的id
                InJectView inject = field.getAnnotation(InJectView.class);
                int id = inject.id();
                if (id > 0) {
                    // 反射访问私有成员，必须加上这句
                    field.setAccessible(true);
                    // 然后对这个属性复制
                    field.set(activity, activity.findViewById(id));
                }
            }
        }
    }

    /**
     * @author sbk
     * @createdate 2014-4-3 下午3:23:08
     * @Description: 关闭加载框
     */
    public void dismisLoading() {
        // if (logoAnimation != null && logoAnimation.isRunning()) { //
        // 如果正在运行,就停止
        // logoAnimation.stop();
        // }
        reLoadLayout.setVisibility(View.GONE);
        ItktLog.i("关闭加载框");
    }

    /**
     * @author sbk
     * @createdate 2014-4-3 下午3:23:08
     * @Description: 显示重新加载
     */
    public void showReLoading() {
        // if (logoAnimation != null && logoAnimation.isRunning()) { //
        // 如果正在运行,就停止
        // logoAnimation.stop();
        // }
        pb.setVisibility(View.GONE);
        reLoadLayout.setVisibility(View.VISIBLE);
        reLoadTv.setVisibility(View.VISIBLE);
    }

    @Override
    public void setContentView(View view) {
        view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        if (mainBody != null) {
            mainBody.removeAllViews();
            mainBody.addView(view);
        }

    }

    @Override
    public void setContentView(View view, LayoutParams params) {
        if (mainBody != null) {
            mainBody.removeAllViews();
            mainBody.addView(view, params);
        }
    }

    /**
     * @param message
     * @param cancelable
     * @param flag
     * @param isLoadingPrompt
     * @author sbk
     * @createdate 2014-5-1 下午6:03:53
     * @Description: 弹出对话框
     */
    public void showWaitingDialog(String message, boolean cancelable, boolean flag, boolean isLoadingPrompt) {
        // if (null == dialog) {
        // dialog = new Dialog(this, R.style.dialog_windowFullscreen);
        // waitingView = LayoutInflater.from(((Activity) this)).inflate(
        // R.layout.view_loading, null);
        // ImageView imageView = (android.widget.ImageView) waitingView
        // .findViewById(R.id.iv_id);
        // logoAnimation = (AnimationDrawable) imageView.getDrawable();
        // dialog.setContentView(waitingView);
        // }
        //
        // if (isLoadingPrompt) { // 使用运营推荐消息
        // // message = getMessage(ItktApplication.LOADING_PROMPT);
        // if (TextUtil.stringIsNull(message)) {
        // message = this.getString(R.string.please_wait);
        // }
        // }
        //
        // if (flag) {
        // waitingView.setBackgroundResource(R.drawable.img_loading_super_bg);
        // } else {
        // waitingView.setBackgroundColor(this.getResources().getColor(
        // R.color.transparent));
        // }
        // waitingView.setBackgroundColor(this.getResources().getColor(
        // R.color.transparent));
        // waitingView.invalidate();
        // // logoAnimation.start(); //开始动画
        // // 使用Handler因为在某些手机不会转
        // if (loadingHandler == null) {
        // loadingHandler = new Handler();
        // }
        // loadingHandler.postDelayed(new Runnable() {
        // public void run() {
        // logoAnimation.start(); // 开始动画
        // }
        // }, 50);
        // // TextView textView = (TextView)
        // waitingView.findViewById(R.id.tv_id);
        // // textView.setText(message);
        // dialog.setCancelable(cancelable);
        // dialog.show();

        pd = ProgressDialog.show(this, null, message, true, cancelable);
    }

    public void dismissWaitingDialog() {
        // if (logoAnimation != null && logoAnimation.isRunning()) { //
        // 如果正在运行,就停止
        // logoAnimation.stop();
        // }
        // dialog.dismiss();
        try {
            pd.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @param task 回调异步操作
     * @author ty
     * @createdate 2012-6-6 下午1:59:16
     * @Description: 提示各种操作的提示语，在非UI线程操作此方法
     */
    @SuppressWarnings("rawtypes")
    private void showUiThreadInfo(final ITask task) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                task.exception();
            }
        });
    }

    /**
     * @author ty
     * @createdate 2013-1-6 上午9:54:10
     * @Description: 没有网络提示信息
     */
    private void notNetWorkInfo() {

        showShortToastMessage(getString(R.string.no_connections));

    }

    /**
     * @param context
     * @param intent
     * @author ty
     * @createdate 2013-1-8 上午11:22:14
     * @Description: 解决产品要求在特定的页面中网络不可能提示用的信息，不使用封装的intentForward方法
     */
    protected void intentForwardNetWork(Context context, int type, Intent intent) {
        if (!EquipmentUtil.checkNetState(context)) {
            notNetWorkInfo();
            return;
        }
        FlyUtil.intentForward(context, intent);
    }

    /**
     * @param <Params>
     * @param <Progress>
     * @param <Result>
     * @author sbk
     * @createdate 2014-5-1 下午5:42:50
     * @Description: 异步任务，可弹对话框
     */
    public abstract class ItktAsyncTaskWithDialog<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> implements
            ITask<Params, Progress, Result> {

        private String message = null;
        private boolean isLoadingPrompt = true; // isLoadingPrompt
        private boolean isCanceled = true;

        public ItktAsyncTaskWithDialog() {
        }

        /**
         * @param message         提示信息
         * @param isLoadingPrompt 是否对话框
         * @param isCanceled      是否允许关闭
         */
        public ItktAsyncTaskWithDialog(String message, boolean isLoadingPrompt, boolean isCanceled) {
            this.message = message;
            this.isLoadingPrompt = isLoadingPrompt;
            this.isCanceled = isCanceled;

        }

        @Override
        protected void onPreExecute() {
            if (isLoadingPrompt) {
                showWaitingDialog(message, isCanceled, false, false);
            }
            super.onPreExecute();
        }

        @Override
        protected Result doInBackground(Params... params) {
            try {
                return before(params);
            } catch (Exception e) {
                e.printStackTrace();
                showUiThreadInfo(this);
                cancel(false);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Result result) {
            super.onPostExecute(result);
            if (isLoadingPrompt) {
                dismissWaitingDialog();
            }
            after(result);
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }
    }

    /**
     * @param <Params>   启动任务执行的参数
     * @param <Progress> 后台任务执行的百分比。
     * @param <Result>   后台执行任务最终返回的结果
     * @author ty
     * @createdate 2012-6-6 下午1:37:40
     * @Description: 异步任务封装---带有ProgressDialog
     */
    public abstract class ItktOtherAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> implements
            ITask<Params, Progress, Result> {

        private boolean isShowProgressDialog = true;

        public ItktOtherAsyncTask() {
        }

        /**
         * 不显示进度对话框
         *
         * @param isShowProgressDialog false：为不显示
         */
        public ItktOtherAsyncTask(boolean isShowProgressDialog) {
            this.isShowProgressDialog = isShowProgressDialog;
        }

        @Override
        protected void onPreExecute() {
            if (isShowProgressDialog) {
                // showLoading();
            }
            super.onPreExecute();
        }

        @Override
        protected Result doInBackground(Params... params) {
            try {
                return before(params);
            } catch (Exception e) {
                e.printStackTrace();
                showUiThreadInfo(this);
                cancel(false);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Result result) {
            super.onPostExecute(result);
            if (isShowProgressDialog) {
                // dismisLoading();
            }
            after(result);
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
            if (isShowProgressDialog) {
            }
        }
    }

    /**
     * @param info
     * @author ty
     * @createdate 2012-10-19 上午11:28:15
     * @Description: 显示Body显示的信息
     */
    protected void showBodyInfo(String info) {
        View view = getLayoutInflater().inflate(R.layout.template_failure, null);
        TextView textView = (TextView) view.findViewById(R.id.txt_load_fail);
        textView.setText(info);
        mainBody.removeAllViews();
        mainBody.setGravity(Gravity.CENTER);
        mainBody.addView(view);
    }

    /**
     * @param info
     * @author ty
     * @createdate 2012-10-19 下午1:55:43
     * @Description: 列表显示数据没有
     */
    protected void showListNoValue(String info) {
        templateView = getLayoutInflater().inflate(R.layout.template_failure, null);
        TextView textView = (TextView) templateView.findViewById(R.id.txt_load_fail);
        textView.setText(info);
        if (mainBody != null) {
            mainBody.removeAllViews();
            mainBody.setGravity(Gravity.CENTER);
            mainBody.addView(templateView);
        }
    }

    /**
     * @param info
     * @author ty
     * @createdate 2012-10-19 下午1:55:43
     * @Description: 列表显示数据没有，这个方法用于列表无数据，可以添加数据，如果无数据就什么都不干，返回重查，直接用上面的方法showListNoValue
     */
    protected void showListNoValueChildGone(String info) {
        templateView = getLayoutInflater().inflate(R.layout.template_failure, null);
        TextView textView = (TextView) templateView.findViewById(R.id.txt_load_fail);
        textView.setText(info);
        int count = mainBody.getChildCount();
        for (int i = 0; i < count; i++) {
            View vi = mainBody.getChildAt(i);
            vi.setVisibility(View.GONE);
        }
        mainBody.setGravity(Gravity.CENTER);
        mainBody.addView(templateView);
        isShowNoValue = true;// 说明执行过现实无数据这个方法，为了有数据后判断，如果执行过这个方法，有数据后才执行showListChildVisible方法里的代码
    }

    /**
     * @author ty
     * @createdate 2012-10-31 下午3:44:42
     * @Description: 查询无数据，再添加数据以后，现实出来添加的数据用此方法；
     * 加了isShowNoValue来判断，意思是执行这个方法体的前提是一定执行过showListNoValueChildGone方法
     */
    protected void showListChildVisible() {
        if (isShowNoValue) {
            mainBody.removeView(templateView);
            templateView = null;
            int count = mainBody.getChildCount();
            for (int i = 0; i < count; i++) {
                View vi = mainBody.getChildAt(i);
                vi.setVisibility(View.VISIBLE);
            }
            isShowNoValue = false;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        //注：回调 1
        Bugtags.onResume(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        //注：回调 2
        Bugtags.onPause(this);
        boolean autoLogin = SharedPreferenceUtil.getBoolean(getBaseContext(), "false");
        if (FlyUtil.isLogin() && !autoLogin) { // 用户已经登录，而设置为"不是自动登录"
            Date date = new Date();
            long nowTime = date.getTime();
            date = null;
            // 超过限定的时限自动退出。
            if (nowTime - FlyApplication.THE_LAST_TIME >= MContants.EXPIRATION_TIME && FlyApplication.THE_LAST_TIME != 0L) {
                ItktLog.w("-----------超过限定的时限自动退出------------->>");
                showShortToastMessage(getString(R.string.timeout_exit));
                FlyApplication.THE_LAST_TIME = 0L;
                // ItktApplication.USER = null;
                FlyApplication.userId = "";
                Intent intent = new Intent(this, MainActivity.class);
                intent.putExtra(MContants.ACTIVITY_RETURN, true);
                intent.putExtra(MContants.TIMEOUT_EXIT_VALIDATE_USER_LOGIN, true);
                FlyUtil.intentForward(this, intent);
            } else {
                FlyApplication.THE_LAST_TIME = nowTime;
            }
        }
    }

    /**
     * @param event
     * @return 集成Bugtags
     * @author xindz
     * @createDate 2016年12月5日10:04:34
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        //注：回调 3
        Bugtags.onDispatchTouchEvent(this, event);
        return super.dispatchTouchEvent(event);
    }


    /**
     * @param title   对话框标题
     * @param message 对话框消息
     * @param btnText 按钮文本
     * @author miaoxin.ye
     * @createdate 2012-9-21 下午3:07:49
     * @Description: 确认对话框
     */
    public void showConfirmDialog(String title, String message, String btnText) {
        if (messageDialog != null) {
            messageDialog.dismiss();
            messageDialog = null;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(btnText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setCancelable(true);
        messageDialog = builder.create();
        messageDialog.show();
    }

    /**
     * @param tipTitle   dialog标题
     * @param tipContent dialog内容
     * @param telphone   电话号码
     * @author ty
     * @createdate 2012-10-26 下午4:58:24
     * @Description: (用一句话描述该方法做什么)
     */
    protected void makingCall(String tipTitle, String tipContent, final String telphone) {
        showConfirmDialog(tipTitle, tipContent, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(MContants.TEL + telphone));
                try {
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * @param title        对话框标题
     * @param message      对话框消息
     * @param sureListener 确认监听器
     * @author miaoxin.ye
     * @createdate 2012-9-24 下午3:34:26
     * @Description: 确认对话框
     */
    public void showConfirmDialog(String title, String message, DialogInterface.OnClickListener sureListener) {
        if (messageDialog != null) {
            messageDialog.dismiss();
            messageDialog = null;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(R.string.btn_sure_text, sureListener);
        builder.setNegativeButton(R.string.btn_cancle, null);
        builder.setCancelable(true);
        messageDialog = builder.create();
        messageDialog.show();
    }

    /**
     * @param title        对话框标题
     * @param message      对话框消息
     * @param sureListener 确认监听器
     * @author ty
     * @createdate 2012-11-20 下午3:30:29
     * @Description: 确认对话框 只有一个按钮
     */
    public void showConfirmDialogPositiveBT(String title, String message, DialogInterface.OnClickListener sureListener) {
        if (messageDialog != null) {
            messageDialog.dismiss();
            messageDialog = null;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(R.string.btn_sure_text, sureListener);
        builder.setCancelable(true);
        messageDialog = builder.create();
        messageDialog.show();
    }

    /**
     * @param title        对话框标题
     * @param message      对话框消息
     * @param sureListener 确认监听器
     * @author ty
     * @createdate 2013-1-22 下午1:44:43
     * @Description: 确认对话框 只有一个按钮 不可点击对话框以为区域
     */
    public void showConfirmDialogPositive(String title, String message, DialogInterface.OnClickListener sureListener) {
        if (messageDialog != null) {
            messageDialog.dismiss();
            messageDialog = null;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(R.string.btn_sure_text, sureListener);
        builder.setCancelable(false);
        messageDialog = builder.create();
        messageDialog.show();
    }

    /**
     * @author bo.wang
     * @createdate 2012-10-22 下午8:17:58
     * @Description
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        ItktLog.w("===================save==========================>>");
        // outState.putSerializable("USER", ItktApplication.USER);
        outState.putString("USER_ID", FlyApplication.userId);

        // LogUtil.d("onSaveInstanceState==>ItktApplication.USER"+ItktApplication.USER);
        // LogUtil.d("onSaveInstanceState==>ItktApplication.USER_ID"+ItktApplication.USER_ID);
        super.onSaveInstanceState(outState);
    }

    /**
     * @author bo.wang
     * @createdate 2012-10-22 下午8:17:58
     * @Description
     */
    protected void onResStoreData(Bundle savedInstanceState) {
        ItktLog.w("================restart=============================>>");
        if (savedInstanceState != null) {
            // ItktApplication.USER = (MemberInfoVo)
            // savedInstanceState.getSerializable("USER");
            FlyApplication.userId = savedInstanceState.getString("USER_ID");

            // LogUtil.d("onResStoreData==>ItktApplication.USER" +
            // ItktApplication.USER);
            // LogUtil.d("onResStoreData==>ItktApplication.USER_ID" +
            // ItktApplication.USER_ID);
        }
    }

    // /**
    // * 拦截MENU
    // */
    // @Override
    // public boolean onMenuOpened(int featureId, Menu menu) {
    // if(mPopupWindow==null){
    // initPopuWindow();
    // }
    // if(!mPopupWindow.isShowing()){
    // /*最重要的一步：弹出显示 在指定的位置(parent) 最后两个参数 是相对于 x / y 轴的坐标*/
    // mPopupWindow.showAtLocation(findViewById(R.id.view_mainBody),
    // Gravity.BOTTOM, 0, 0);
    // }
    // return false;// 返回为true 则显示系统menu
    // }

	/*
     * private void initPopuWindow(){ 设置显示menu布局 view子VIEW View subView =
	 * getLayoutInflater().inflate(R.layout.menu_popup, null); 第一个参数弹出显示view
	 * 后两个是窗口大小 mPopupWindow = new PopupWindow(subView,
	 * LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); //
	 * mPopupWindow.setAnimationStyle(R.style.popup_in_out); 设置背景显示
	 * mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); 设置触摸外面时消失
	 * mPopupWindow.setOutsideTouchable(true); 设置系统动画
	 * mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
	 * mPopupWindow.update(); mPopupWindow.setTouchable(true);
	 * 设置点击menu以外其他地方以及返回键退出 mPopupWindow.setFocusable(true);
	 *//**
     * 1.解决再次点击MENU键无反应问题 2.sub_view是PopupWindow的子View
     */
    /*
     * subView.setFocusableInTouchMode(true); subView.setOnKeyListener(new
	 * OnKeyListener() {
	 * 
	 * @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if
	 * ((keyCode == KeyEvent.KEYCODE_MENU)&&(mPopupWindow.isShowing())) {
	 * mPopupWindow.dismiss();// 这里写明模拟menu的PopupWindow退出就行 return true; }
	 * return false; } }); 监听MENU事件 Button aboutBut = (Button)
	 * subView.findViewById(R.id.about_id); aboutBut.setOnClickListener(new
	 * View.OnClickListener() {
	 * 
	 * @Override public void onClick(View v) { Intent intent=new Intent();
	 * intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
	 * ItktUtil.intentForward(AbstractActivity.this, AboutUsActivity.class,
	 * intent); mPopupWindow.dismiss(); } }); Button helpBut = (Button)
	 * subView.findViewById(R.id.help_id); helpBut.setOnClickListener(new
	 * View.OnClickListener() {
	 * 
	 * @Override public void onClick(View v) { Intent intent=new Intent();
	 * intent.putExtra(IntentConstants.TYPE, IntentConstants.USE_HELP);
	 * intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
	 * ItktUtil.intentForward(AbstractActivity.this, WapClientActivity.class,
	 * intent); mPopupWindow.dismiss(); } }); Button opinionBut = (Button)
	 * subView.findViewById(R.id.opinion_id); opinionBut.setOnClickListener(new
	 * View.OnClickListener() {
	 * 
	 * @Override public void onClick(View v) { mPopupWindow.dismiss();
	 * if(ItktApplication.IS_FIRST){ //第一次使用，该功能是用户注册
	 * ItktUtil.intentForward(AbstractActivity.this, RegActivity.class); return;
	 * } Intent intent=new Intent(); if(ItktUtil.isLogin()){
	 * intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
	 * ItktUtil.intentForward(AbstractActivity.this, FeedBackActivity.class,
	 * intent); }else{
	 * ItktUtil.intentForwardValidateLogin(AbstractActivity.this,
	 * FeedBackActivity.class, intent); } } }); Button exitBut = (Button)
	 * subView.findViewById(R.id.exit_id); exitBut.setOnClickListener(new
	 * View.OnClickListener() {
	 * 
	 * @Override public void onClick(View v) { ItktUtil.exitApp();
	 * android.os.Process.killProcess(android.os.Process.myPid()); } }); }
	 */

	/*
     * @Override public boolean onCreateOptionsMenu(Menu menu) {
	 * super.onCreateOptionsMenu(menu); // menu.add(Menu.NONE, Menu.FIRST+1, 1,
	 * Constants.MENU_NAME[0]).setIcon(R.drawable.img_menu_login);
	 * menu.add(Menu.NONE, Menu.FIRST+2, 2,
	 * Constants.MENU_NAME[0]).setIcon(R.drawable.img_menu_about);
	 * menu.add(Menu.NONE, Menu.FIRST+3, 3,
	 * Constants.MENU_NAME[1]).setIcon(R.drawable.img_menu_help);
	 * menu.add(Menu.NONE, Menu.FIRST+4, 4,
	 * Constants.MENU_NAME[2]).setIcon(R.drawable.img_menu_opinion);
	 * menu.add(Menu.NONE, Menu.FIRST+5, 5,
	 * Constants.MENU_NAME[3]).setIcon(R.drawable.img_menu_exit); return true; }
	 */

	/*
     * @Override public boolean onOptionsItemSelected(MenuItem item) { Intent
	 * intent; switch (item.getItemId()) { // case Menu.FIRST+1: //登录/注册 //
	 * showShortToastMessage(Constants.MENU_NAME[0]); // break; case
	 * Menu.FIRST+2: //关于我们 intent=new Intent();
	 * intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
	 * ItktUtil.intentForward(this, AboutUsActivity.class, intent); break; case
	 * Menu.FIRST+3: //使用帮助 intent=new Intent();
	 * intent.putExtra(IntentConstants.TYPE, IntentConstants.USE_HELP);
	 * intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
	 * ItktUtil.intentForward(this, WapClientActivity.class, intent); break;
	 * case Menu.FIRST+4: //意见反馈 if(ItktApplication.IS_FIRST){ //第一次使用，该功能是用户注册
	 * ItktUtil.intentForward(this, RegActivity.class); break; } intent=new
	 * Intent(); if(ItktUtil.isLogin()){
	 * intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
	 * ItktUtil.intentForward(this, FeedBackActivity.class, intent); }else{
	 * ItktUtil.intentForwardValidateLogin(this,FeedBackActivity.class, intent);
	 * } break; case Menu.FIRST+5: //退出软件 ItktUtil.exitApp();
	 * android.os.Process.killProcess(android.os.Process.myPid()); break; }
	 * return false; }
	 */

    /**
     * @return
     * @author miaoxin.ye
     * @createdate 2012-12-17 下午2:07:26
     * @Description: 随机获取运维推广的广告语消息
     */
    /*
	 * private String getMessage(String[] messageList){ if(messageList!=null &&
	 * messageList.length>0){ Random random = new Random(); int i =
	 * random.nextInt(messageList.length-1); return messageList[i]; } return
	 * null; }
	 */

    // 停止不用的线程
    public void cancelTask(AsyncTask task) {
        if (task != null && task.getStatus() == Status.RUNNING) {
            task.cancel(true);
            task = null;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    // 退出栈顶Activity
    public void popActivity(Activity activity) {
        if (activity != null) {
            // 在从自定义集合中取出当前Activity时，也进行了Activity的关闭操作
            activity.finish();
            activityStack.remove(activity);
            activity = null;
        }
    }

    // 获得当前栈顶Activity
    public Activity currentActivity() {
        Activity activity = null;
        if (!activityStack.empty())
            activity = activityStack.lastElement();
        return activity;
    }

    // 将当前Activity推入栈中
    public void pushActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    // 退出栈中所有Activity
    public void popAllActivityExceptOne(Class<?> cls) {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            if (activity.getClass().equals(cls)) {
                break;
            }
            popActivity(activity);
        }
    }

    @Override
    protected void onDestroy() {
        mainBody = null;
        FlyUtil.dismissAlert(dialog);
        FlyUtil.dismissAlert(messageDialog);
        FlyUtil.removeAppActivity(this);
        // mController.unregisterListener(listener);
        super.onDestroy();
    }

    @Override
    public void finish() {
        super.finish();
        if (!finishAnim) {
            overridePendingTransition(R.anim.activity_on, R.anim.activity_off);
        }
    }
}
